﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class TabAddLine : Base
    {
        protected int Ball1Value
        {
            get { return Int32.Parse(this.txtBall1.Text); }
        }
        protected int Ball2Value
        {
            get { return Int32.Parse(this.txtBall2.Text); }
        }
        protected int Ball3Value
        {
            get { return Int32.Parse(this.txtBall3.Text); }
        }
        protected int Ball4Value
        {
            get { return Int32.Parse(this.txtBall4.Text); }
        }
        protected int Ball5Value
        {
            get { return Int32.Parse(this.txtBall5.Text); }
        }
        protected int Ball6Value
        {
            get { return Int32.Parse(this.txtBall6.Text); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            if(this.DrawLocked)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=2", this.CurrentDraw.id));

            int minimumFunds = 1;
            if (this.CurrentDraw.type == draw.drawType.EUROMILLIONS)
                minimumFunds = 2;

            if ((int)this.CurrentDraw.pricePerLine != 0)
            {
                minimumFunds = (int) this.CurrentDraw.pricePerLine;
            }

            if (this.RemainingFunds < minimumFunds)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=1", this.CurrentDraw.id));

            if (this.RemainingFunds == minimumFunds)
            {
                this.ButtonAddAnotherLine.Visible = false;
            }

        }

        protected void ButtonAddLine_Click(object sender, EventArgs e)
        {

            if (!Page.IsValid)
                return;

            List<int> balls = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);
            if (!balls.Contains(this.Ball6Value))
                balls.Add(this.Ball6Value);

            if (balls.Count != 6)
                return;

            balls.Sort();

            bool result = SaveForm(balls);


            if (result)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=2", this.DrawId));
            else
            {
                FailurePlaceHolder.Visible = true;
            }
        }

        private bool SaveForm(List<int> balls)
        {
            line l = new line();

            l.drawId = this.CurrentDraw.id;
            l.numbers = String.Join(",", balls.ToArray());

            if (this.BasePage.BusinessFacade.LineBL.InsertLine(l))
            {
                InsertEventLogEntry("Line [" + l.id.ToString() + "] added.");

                return true;
            }
            else
                return false;



        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            List<int> balls = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);
            if (!balls.Contains(this.Ball6Value))
                balls.Add(this.Ball6Value);

            balls.Sort();

            if (balls.Count != 6)
            {
                this.CustomValidator1.ErrorMessage = "Please remove duplicated numbers.";

                args.IsValid = false;
            }

            if (!args.IsValid)
                return;

            foreach (int b in balls)
            {
                if (b >= 50 || b <= 0)
                {
                    this.CustomValidator1.ErrorMessage = "Please enter numbers in range 1-49.";

                    args.IsValid = false;
                    break;
                }
            }

            if (!args.IsValid)
                return;

            string numbers = String.Join(",", balls.ToArray());

            int count = this.BasePage.BusinessFacade.LineBL.SelectAllByDrawId(CurrentDraw.id).ToList().Where(d => d.numbers == numbers).Count();

            if (count > 0)
            {
                this.CustomValidator1.ErrorMessage = "Line with selected numbers is already added to this draw.";
                args.IsValid = false;
            }

           

        }

        protected void ButtonAddAnotherLine_Click(object sender, EventArgs e)
        {
            List<int> balls = new List<int>();

            balls.Add(this.Ball1Value);

            if (!balls.Contains(this.Ball2Value))
                balls.Add(this.Ball2Value);
            if (!balls.Contains(this.Ball3Value))
                balls.Add(this.Ball3Value);
            if (!balls.Contains(this.Ball4Value))
                balls.Add(this.Ball4Value);
            if (!balls.Contains(this.Ball5Value))
                balls.Add(this.Ball5Value);
            if (!balls.Contains(this.Ball6Value))
                balls.Add(this.Ball6Value);

            if (balls.Count != 6)
                return;

            balls.Sort();

            bool result = SaveForm(balls);

            string numbers = String.Join(",", balls.ToArray());

            if (result)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=3&previous={1}", this.DrawId, numbers));
            else
            {
                FailurePlaceHolder.Visible = true;
            }
        }

        protected string PrintPreviousNumber()
        {
            if (Page.Request.QueryString["previous"] != null)
            {
                return String.Format("Last numbers added: {0}", Page.Request.QueryString["previous"].ToString());
            }
            else
                return "";
        
        }

        protected string PrintFirstFieldId()
        {
            return this.txtBall1.ClientID;

        }

    }
}